package com.xianzhiOA.controller;

import com.xianzhiOA.bean.*;
import com.xianzhiOA.service.*;
import com.xianzhiOA.service.impl.XzScheduleServiceImpl;
import com.xianzhiOA.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * ScheduleController
 *
 * @author 甘汝雷
 * @create 2017-04-19 10:17
 **/
@Controller
@RequestMapping("/Schedule")
@SessionAttributes("userLogin")
public class XzScheduleController {
    @Autowired
    private XzScheduleService scheduleService;
    @Autowired
    private XzWorkService workService;
    @Autowired
    private XzEmpService empService;
    @Autowired
    private XzProjectService projectService;
    @Autowired
    private XzPlanService planService;
    @Autowired
    private XzModuleService moduleService;

    //分页查询进度信息
    @RequestMapping("selScheduleAll.emp")
    public ModelAndView selScheduleAll(XzSchedule schedule, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer rows,
                                       Date begin,Date beginDate, Date endDate,@ModelAttribute("userLogin")XzEmp emp) {
        ModelAndView mv = new ModelAndView("backEnd/scheduleShow");
        if (schedule.getEmpId()==null)schedule.setEmpId(emp.getEmpId());
        PageBean<XzSchedule> pageBean = scheduleService.selScheduleAll(schedule, page, rows, beginDate, endDate,begin);
        List<XzSchedule> list = pageBean.getList();
        if (!list.isEmpty()) {
            int i = 0, j = 0, k = 0;
            for (XzSchedule s : list) {
                Map map1 = workService.getWeekByDate(s.getScheduleTime());
                s.setScheduleBegin((String) map1.get("begin"));
                s.setScheduleEnd((String) map1.get("end"));
                i += s.getSchedulePlan();
                j += s.getScheduleActual();
                k++;
            }
            schedule.setModuleBegin(i / k);
            schedule.setModuleEnd(j / k);
        }
        mv.addObject("scheduleList", pageBean.getList());
        mv.addObject("schedule", schedule);
        mv.addObject("page", pageBean.getPageNum());
        mv.addObject("pages", pageBean.getPages());
        mv.addObject("total", pageBean.getTotal());
        mv.addObject("beginDate", beginDate);
        mv.addObject("endDate", endDate);
        mv.addObject("begin", begin);
        return mv;
    }

    //添加进度信息
    @RequestMapping("addSchedule.emp")
    public ModelAndView addSchedule(XzSchedule schedule,@ModelAttribute("userLogin")XzEmp emp) {
        ModelAndView mv = errors(schedule);
        if (mv.getViewName().equals("backEnd/scheduleEdit"))return mv;
        scheduleService.addSchedule(schedule);
        return mv;
    }

    //完善进度信息
    public XzSchedule mySchedule(XzSchedule schedule){
        Map map = new HashMap();
        map.put("moduleEmp",schedule.getEmpId());
        map.put("moduleProject",schedule.getScheduleProject());
        map.putAll(workService.getWeekByDate(schedule.getScheduleTime()));
        List<XzModule> moduleList = moduleService.selAll(map);
        int i=0,j=0,k=0;//计划比例、实际比例、模块信息数
        if (!moduleList.isEmpty()) {
            for (XzModule m : moduleList) {
                k++;
                i += Integer.valueOf(m.getModulePlan());
                j += Integer.valueOf(m.getModuleActual());
            }
            i = i / k;
            j = j / k;
        }
        schedule.setSchedulePlan(i);
        schedule.setScheduleActual(j);
        return schedule;
    }
    //编辑进度
    @RequestMapping("scheduleEdit.emp")
    public ModelAndView scheduleEdit(long scheduleId, @ModelAttribute("userLogin")XzEmp emp) {
        ModelAndView mv = new ModelAndView("backEnd/scheduleEdit");
        if (scheduleId != 0) {
            XzSchedule schedule = scheduleService.selByScheduleId(scheduleId);
            mv.addObject("schedule", schedule);
        } else {
            XzSchedule schedule = new XzSchedule();
            schedule.setScheduleId(0L);
            schedule.setEmpId(emp.getEmpId());
            mv.addObject("schedule", schedule);
        }
        mv.addObject("projectList",projectService.selAll(new XzProject()));
        return mv;
    }

    //表单验证
    public ModelAndView errors(XzSchedule schedule){
        ModelAndView mv = new ModelAndView("redirect:selScheduleAll.emp");
        String scheduleTime = "";
        if (schedule.getScheduleTime()==null)scheduleTime = "所在周期不能为空";
        schedule = mySchedule(schedule);
        if (schedule.getSchedulePlan()==0)
            scheduleTime = "当前周期未创建模块不可用";
        Map byDate = workService.getWeekByDate(schedule.getScheduleTime());
        List<XzSchedule> scheduleList = scheduleService.selScheduleAll(byDate);
        if (!scheduleList.isEmpty()){
            for (XzSchedule s:scheduleList) {
                if (s.getEmpId()==schedule.getEmpId()&&s.getScheduleProject()==schedule.getScheduleProject()) {
                    if (s.getScheduleId()!=schedule.getScheduleId()) scheduleTime = "当前周期的进度信息已存在不可修改";
                    if (schedule.getScheduleId()==0) scheduleTime = "当前周期的进度信息已存在不可添加";
                }
            }
        }
        if (scheduleTime!=""){
            mv.addObject("schedule",schedule);
            mv.addObject("scheduleTime",scheduleTime);
            mv.setViewName("backEnd/scheduleEdit");
            mv.addObject("projectList",projectService.selAll(new XzProject()));
            mv.addObject("empList",empService.selAll(new XzEmp()));
            return mv;
        }
        return mv;
    }
    @RequestMapping("updateSchedule.emp")
    public ModelAndView updateSchedule(XzSchedule schedule) {
        ModelAndView mv = errors(schedule);
        if (mv.getViewName().equals("backEnd/scheduleEdit"))return mv;
        scheduleService.update(schedule);
        return mv;
    }

    //删除进度信息
    @RequestMapping("deleteSchedule.emp")
    public ModelAndView deleteSchedule(Long scheduleId){
        ModelAndView mv = new ModelAndView("redirect:selScheduleAll.emp");
            scheduleService.isdelete(scheduleId);
        return mv;
    }
}
